package com.afei.oid;


import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.Toast;

public class AudioServer extends Service {
	public static final String TAG = "JENSOR_ServoServer";

	private Messenger mAnswerMessenger;
	private ActionHandle mActionHandler;
	private Messenger mSendMessenger = null;
	private int mPageCount;
	private String mFile;
	private int mAudioOffset;
	private GetPlayInfo mGetPlayInfo;
	private PlayAudio mPlayAudio;
	private int mCurrentPage;
	private EbookConstants.AutoType mCurrentAutoStatus;
	private SentenceInfo mAutoStartSentence;
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
        mActionHandler = new ActionHandle();
        mSendMessenger = new Messenger(mActionHandler); 
        Log.i(TAG, "ServoServer onCreate");
		super.onCreate();
	}
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		Log.i(TAG, "ServoServer onDestroy");
		super.onDestroy();
	}
	@Override
	public void onLowMemory() {
		// TODO Auto-generated method stub
		super.onLowMemory();
	}
	@Override
	public void onRebind(Intent intent) {
		// TODO Auto-generated method stub
		super.onRebind(intent);
	}
	private void Initial()
	{
		String db = FactoryClass.OidFile2DbFile(mFile);
		mGetPlayInfo = FactoryClass.FactoryGetPlayInfo(this,db,mPageCount);
		mPlayAudio = new PlayAudio();
		mPlayAudio.InitFile(mFile, mAudioOffset);
	}
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		Log.i(TAG, "ServoServer onStartCommand");
	     mPageCount = intent.getIntExtra(EbookConstants.VALUEPAGE, 0);
        mFile = intent.getStringExtra(EbookConstants.VALUEFILE);
        mAudioOffset= intent.getIntExtra(EbookConstants.VALUEOFFSET,0);
        if(mPageCount > 0 && mFile != null && !mFile.equals("") && mAudioOffset > 0)
        {
        	Initial();
        }
		return super.onStartCommand(intent, flags, startId);
	}
//	@Override
//	public void onTaskRemoved(Intent rootIntent) {
//		// TODO Auto-generated method stub
//		super.onTaskRemoved(rootIntent);
//	}
//	@Override
//	public void onTrimMemory(int level) {
//		// TODO Auto-generated method stub
//		super.onTrimMemory(level);
//	}
	@Override
	public boolean onUnbind(Intent intent) {
		// TODO Auto-generated method stub
		Log.i(TAG, "ServoServer onUnbind");
		return super.onUnbind(intent);
	}

	class ActionHandle extends Handler{

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
	    	SentenceInfo sentence = (SentenceInfo)(msg.obj);
	    	EbookConstants.TouchEvent event = EbookConstants.TouchEvent.values()[msg.what];
	    	switch(event)
	    	{
	    	case EVENT_UPDATEPAGE:
	    		if(msg.arg1 > 0 && msg.arg1 <= mPageCount)
	    		{
	    			mCurrentPage = msg.arg1;
	    			mGetPlayInfo.UpdatePageMap(mCurrentPage);
	    		}
	    		break;
	    	case EVENT_TOUCH:
//	    		EbookConstants.TouchEvent newevent = mGetPlayInfo.GetTouchEvent(msg.arg1, msg.arg2, mCurrentPage);
//	    		if(newevent != EbookConstants.TouchEvent.EVENT_NONE)
//	    		{
//	    			Message newmsg = Message.obtain(mActionHandler, newevent.ordinal());
//		    		newmsg.arg1 = mGetPlayInfo.GetTouchIndex();
//		    		mActionHandler.sendMessage(newmsg);
//		    		mPlayAudio.Stop();
//		    		if(newevent == EbookConstants.TouchEvent.EVENT_BOOK_PLAY 	||
//		    		newevent == EbookConstants.TouchEvent.EVENT_CHAPPTER_PLAY 	||
//		    		newevent == EbookConstants.TouchEvent.EVENT_DBCLICK_PLAY 	||
//		    		newevent == EbookConstants.TouchEvent.EVENT_PAGE_PLAY)
//		    		{
//		    			mAutoStartSentence = mGetPlayInfo.GetCurrentSentence(newmsg.arg1);
//		    		}
//		    		else
//		    		{
//		    			mAutoStartSentence = null;
//		    		}
//	    		}
	    		break;
	    	case EVENT_NONE:
	    		break;
	    	case EVENT_SINGLE_PLAY:
	    		SentenceInfo playsent = mGetPlayInfo.GetCurrentSentence(msg.arg1);
	    		if(playsent.mPlayLength > 1 && playsent.mPlayStart > 1)
	    		{
	    			mPlayAudio.PlayBuffer(playsent.mPlayStart, playsent.mPlayLength);
	    			mCurrentAutoStatus = EbookConstants.AutoType.AUTO_SINGLE;
	    		}
	    		break;
	    	case EVENT_PAGE_PLAY:
	     		mCurrentAutoStatus = EbookConstants.AutoType.AUTO_PAGE;
	    		if(mAutoStartSentence != null)
	    		{
	    			mAutoStartSentence.mAutoSentenceType = EbookConstants.AutoType.AUTO_PAGE;
	    		}
	    		break;
	    	case EVENT_DBCLICK_PLAY:
	    		mCurrentAutoStatus = EbookConstants.AutoType.AUTO_PAGE;
	    		if(mAutoStartSentence != null)
	    		{
	    			mAutoStartSentence.mAutoSentenceType = EbookConstants.AutoType.AUTO_PAGE;
	    		}
	    		break;
	    	case EVENT_CHAPPTER_PLAY:
	    		mCurrentAutoStatus = EbookConstants.AutoType.AUTO_CHAPTER;
	    		if(mAutoStartSentence != null)
	    		{
	    			mAutoStartSentence.mAutoSentenceType = EbookConstants.AutoType.AUTO_CHAPTER;
	    		}
	    		break;
	    	case EVENT_BOOK_PLAY:
	    		mCurrentAutoStatus = EbookConstants.AutoType.AUTO_BOOK;
	    		if(mAutoStartSentence != null)
	    		{
	    			mAutoStartSentence.mAutoSentenceType = EbookConstants.AutoType.AUTO_BOOK;
	    		}
	    		break;
	    	case EVENT_STOP:
	    		break;
	    	case EVENT_AUTOCONTINUE:
	    		break;
	    	default:
	    		break;
	    	}
		}
	}
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		Log.i(TAG, "ServoServer onBind");
		return mSendMessenger.getBinder();
	}
}
